﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Concurrent;
using System.Data.Odbc;

namespace QuanLyQuangCao
{
    public partial class frmDangKy_View : Form
    {
        public static frmDangKy_View View;
        //public Connection layDuLieu = new Connection();      

        public string bButton = "";
        public SqlDataAdapter da = new SqlDataAdapter();
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();   
        public BindingSource bds = new BindingSource();

        public frmDangKy_View()
        {
            View = this;
            InitializeComponent();
        }
        SqlConnection Conn = new SqlConnection(C_Conn.Connection);
        public void frmDangKy_View_Load(object sender, EventArgs e)
        {
            Conn.Open();
            string chuoi = @"SELECT Ma_Dang_Ky, Ma_Khach, Ma_loai, Gia_Mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien FROM DangKyQuangCao";

            //layDuLieu(chuoi, ds, "DangKyQuangCao");// lay du lieu frm frmChinh
            
            da = new SqlDataAdapter(chuoi, Conn);            
            da.Fill(ds, "DangKyQuangCao");
            dt = ds.Tables["DangKyQuangCao"];
            //Sua lai ten cot trong dgv
            GetColumn(dt);

            bds.DataSource = dt;
            dgvDangKy_View.DataSource = bds;
            
            //
            dgvDangKy_View.ReadOnly = true;
            dgvDangKy_View.Columns[0].Visible = false;         
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            bButton = "Them";                
            frmDangKy_Edit frm = new frmDangKy_Edit();
            frm.ShowDialog();           
        }        
        public void Update(BindingSource bds)
        {
            dgvDangKy_View.DataSource = bds;            
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            bButton = "Sua";
            frmDangKy_Edit frm = new frmDangKy_Edit();
            frm.ShowDialog();
        }        
        
        public BindingSource bdsGridView()
        {
            DataTable dtRow = new DataTable();
            da.Fill(dtRow );
            DataGridViewRow dgvViewRow = dgvDangKy_View.CurrentRow;
            DataRow drNewRow = dtRow.NewRow();

            drNewRow[0] = Convert.ToInt16(dgvViewRow.Cells[0].Value);
            drNewRow[1] = Convert.ToString(dgvViewRow.Cells[1].Value);
            drNewRow[2] = Convert.ToString(dgvViewRow.Cells[2].Value);
            drNewRow[3] = Convert.ToInt16(dgvViewRow.Cells[3].Value);
            drNewRow[4] = Convert.ToInt16(dgvViewRow.Cells[4].Value);
            drNewRow[5] = Convert.ToDateTime(dgvViewRow.Cells[5].Value);
            drNewRow[6] = Convert.ToString(dgvViewRow.Cells[6].Value);


            //drNewRow["Ma_Dang_Ky"] = Convert.ToInt16(dgvViewRow.Cells["Ma_Dang_Ky"].Value);
            //drNewRow["Ma_Khach"] = Convert.ToString(dgvViewRow.Cells["Ma_Khach"].Value);
            //drNewRow["Ma_Loai"] = Convert.ToString(dgvViewRow.Cells["Ma_Loai"].Value);
            //drNewRow["Gia_Mot_Ky"] = Convert.ToInt16(dgvViewRow.Cells["Gia_Mot_Ky"].Value);
            //drNewRow["So_Ky_Dang"] = Convert.ToInt16(dgvViewRow.Cells["So_Ky_Dang"].Value);
            //drNewRow["Ngay_Bat_Dau"] = Convert.ToDateTime(dgvViewRow.Cells["Ngay_Bat_Dau"].Value);
            //drNewRow["Ma_Nhan_Vien"] = Convert.ToString(dgvViewRow.Cells["Ma_Nhan_Vien"].Value);

            dtRow.Rows.Clear();            
            dtRow.Rows.Add(drNewRow);
            //Sua lai ten cot
            GetColumn(dtRow);
            BindingSource bdsRow = new BindingSource();
            bdsRow.DataSource = dtRow;
            return bdsRow;
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            string sqlXoa = @"DELETE FROM DangKyQuangCao WHERE Ma_Dang_Ky = @Ma_Dang_Ky";
            SqlCommand cmdXoa = new SqlCommand(sqlXoa, Conn);
            cmdXoa.Parameters.Add("@Ma_Dang_Ky", SqlDbType.Int);
            cmdXoa.Parameters["@Ma_Dang_Ky"].Value = dgvDangKy_View.CurrentRow.Cells[0].Value;
            cmdXoa.ExecuteNonQuery();

            //Gan lai source cho dvg
            dt.Clear();
            string chuoi = @"SELECT Ma_Dang_Ky, Ma_Khach, Ma_loai, Gia_Mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien FROM DangKyQuangCao";
            da = new SqlDataAdapter(chuoi, Conn);
            da.Fill(dt);            
            bds.DataSource = dt;
            dgvDangKy_View.DataSource = bds;

            MessageBox.Show("Đã thực hiện xong", "Thông báo", MessageBoxButtons.OK);                
        }
        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();   
        }
        public void GetColumn(DataTable  dtGetColumn)
        {
            dtGetColumn.Columns["Ma_Dang_Ky"].ColumnName = "Mã đăng ký";
            dtGetColumn.Columns["Ma_Khach"].ColumnName = "Mã Khách";
            dtGetColumn.Columns["Ma_Loai"].ColumnName = "Mã Loại";
            dtGetColumn.Columns["Gia_Mot_Ky"].ColumnName = "Giá một kỳ";
            dtGetColumn.Columns["So_Ky_Dang"].ColumnName = "Số kỳ đăng";
            dtGetColumn.Columns["Ngay_Bat_Dau"].ColumnName = "Ngày bắt đầu";
            dtGetColumn.Columns["Ma_Nhan_Vien"].ColumnName = "Mã nhân viên";
        }

    }
}
